/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package genomemap.provider.impl;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
import org.xml.sax.SAXException;

import java.io.File;

/**
 * @author Susanta Tewari
 */
public final class XMLUtil {

    /**
     * Constructs ...
     *
     */
    private XMLUtil() {}

    /**
     * Method description
     *
     *
     * @param file
     *
     * @throws Exception
     */
    public static void validateSchema(final File file) throws Exception {


        // 2. create Document from file
        final SAXReader reader = new SAXReader();
        Document document      = null;

        try {


            // 2.1 settings to check the schema
            reader.setValidation(true);
            reader.setFeature("http://xml.org/sax/features/validation", true);
            reader.setFeature("http://apache.org/xml/features/validation/schema", true);
            reader.setFeature("http://apache.org/xml/features/validation/schema-full-checking",
                              true);
            reader.setFeature("http://apache.org/xml/features/continue-after-fatal-error", false);


            // 2.2 check the schema and read the file
            document = reader.read(file);


            // wraps DocumentException (from reader.read) and SAXException (from reader.setFeature)
        } catch (SAXException | DocumentException ex) {
            throw new Exception(ex);
        }

        if (document == null) {
            throw new Exception("Error in parsing file " + file.getAbsolutePath());
        }
    }
}
